<?php
require '../../common/initPage.php';
require '../../common/dbcon.php';
require '../../common/func.php';
require '../../common/showTips.php';
require '../../common/checkForm.php';

session_start();
//拦截未登录
filterLogout();

unset($_SESSION['actMessDetailShowTips']);
$messageID=$_POST['messageID'];
if(empty($_GET['messageID'])){
    $messageID=$_POST['messageID'];
}else{
    $messageID=$_GET['messageID'];
}
$messageContent=queryInfoByID($mysqlCon,queryMessByMId,$messageID);
//发送人信息
$messFromWhoInfo=queryInfoByID($mysqlCon,queryisNull,$messageContent[3]);
//接收人信息
$messToWhoInfo=queryInfoByID($mysqlCon,queryisNull,$messageContent[4]);
//将消息自动标为已读
if(empty($messageContent[7])&&$messageContent[3]!=$_SESSION['loginId']){
    upULLT($mysqlCon,updataMessIsRead,$messageID);
    //并更新页头的未读消息
    $_SESSION['loginUnReadMessInfo']=queryUnReadMess($mysqlCon,queryBrowseLNewButUnRead,$_SESSION['loginId']);
}

if(!empty($_POST['action'])){
    if ($_POST['action']=='star'){
        if(uplockUser($mysqlCon,updteMessisStar,$_POST['dealwitharray'])){
            $_SESSION['actMessDetailShowTips']='收藏消息成功';
            $_SESSION['actMessDetailtipsType']='success';
            $messageContent[5]=1;
        }else{
            $_SESSION['actMessDetailShowTips']='收藏消息失败';
            $_SESSION['actMessDetailtipsType']='danger';
        }
    }else if ($_POST['action']=='unstar'){
        if(uplockUser($mysqlCon,updteMessisStar,$_POST['dealwitharray'])){
            $_SESSION['actMessDetailShowTips']='取消收藏消息成功';
            $_SESSION['actMessDetailtipsType']='success';
            $messageContent[5]=0;
        }else{
            $_SESSION['actMessDetailShowTips']='取消收藏消息失败';
            $_SESSION['actMessDetailtipsType']='danger';
        }
    }
}

if(!empty($_GET['action'])){
    if($_GET['action']=='move'){
        $moveMessToTrash="UPDATE usermessage SET messstate = 2 WHERE mid = ".$_GET['dealwitharray'];
        if(upMess($mysqlCon,$moveMessToTrash)){
            $_SESSION['actionMessShowTips']='已成功将消息删除，一个月内可从回收站复原';
            $_SESSION['actionMesstipsType']='success';
            //不能很好地处理删除最后一页最后一条数据带来地页面变更影响 可以尝试跳回第一页或复杂地传递二层的数据
            header('location:./message.php?&show=true&pagenow='.$_SESSION['messagePageNow'].'&searchvalue='.$_SESSION['messageSearchvalue'].'&check='.$_SESSION['checkCate'].'&filter='.$_SESSION['filter']);
        }else{
            $_SESSION['actionMessShowTips']='消息删除失败';
            $_SESSION['actionMesstipsType']='danger';
        }
    }
}

$routerUrl = '../../views/public/browsemess.html';
if($_SESSION['loginIden']=='管理员'){
    require '../../views/admin/template.html';
}else{
    require '../../views/user/template.html';
}